﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class RasporedProf : System.Web.UI.Page
{

    public struct Niza
    {
        public List<string> den;
        public List<string> od;
        public List<string> doo;
        public List<string> aupr;
        public List<string> predmet;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((int)Session["tip"] != 2) Response.Redirect("NO.aspx");
        else Session["strana"] = Request.RawUrl.ToString();
        if (!IsPostBack)
        {
            lblIme.Text = "Име: " + Session["ime"].ToString();
            lblPrezime.Text = "Презиме: " + Session["prezime"].ToString();     
        }
        Like_Totally_Proffesor_Funkcija();
    }

    /// <summary>
    ///     <para>
    ///         Like_Totally_Proffesor_Funkcija funkcijata ja povikuva funkcijata ispolni koja vraka 6 listi.
    ///         Tie se koristat za dinamicki da se kreira HTML tabela koja gi sodrzi predavanjata i
    ///         konsultaciite koi gi ima logiraniot profesor.
    ///         Funkcijata ja izminuva tabelata po redici, i za sekoj blok cas, go azurira column span-ot na soodvetnata kelija.
    ///     </para>
    /// </summary>
    protected void Like_Totally_Proffesor_Funkcija()
    {
         // Ja povikuvame funkcijata rasp koja vraka objekt Niza. Kreirame 6 listi vo koi gi stavame listite od objektot Niza
        Niza raspored = ispolni();
        List<string> den = raspored.den;
        List<string> od = raspored.od;
        List<string> doo = raspored.doo;
        List<string> aupr = raspored.aupr;
        List<string> predmet = raspored.predmet;

        int brojacDolzinaNaBlokCas1 = 0, brojacDolzinaNaBlokCas2 = 0, brojacDolzinaNaBlokCas3 = 0, brojacDolzinaNaBlokCas4 = 0, brojacDolzinaNaBlokCas5 = 0;
        string tipPredavanje1 = string.Empty, tipPredavanje2 = string.Empty, tipPredavanje3 = string.Empty, tipPredavanje4 = string.Empty, tipPredavanje5 = string.Empty, predmet1 = string.Empty, predmet2 = string.Empty, predmet3 = string.Empty, predmet4 = string.Empty, predmet5 = string.Empty;
        // Poceten html kod
        pnlPanela.Controls.Add(new LiteralControl("<table id='tabela' >"));
        pnlPanela.Controls.Add(new LiteralControl("<colgroup>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='even'/>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='odd'/>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='even'/>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='odd'/>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='even'/>"));
        pnlPanela.Controls.Add(new LiteralControl("<col class='odd'/>"));
        pnlPanela.Controls.Add(new LiteralControl("</colgroup>"));
        pnlPanela.Controls.Add(new LiteralControl("<thead><tr><th align='center' scope='col' id='par'>Час</th>"));
        pnlPanela.Controls.Add(new LiteralControl("<th scope='col' align='center' id='nep'>Понеделник</th>"));
        pnlPanela.Controls.Add(new LiteralControl("<th scope='col' id='par' align='center'>Вторник</th>"));
        pnlPanela.Controls.Add(new LiteralControl("<th scope='col' id='nep' align='center'>Среда</th>"));
        pnlPanela.Controls.Add(new LiteralControl("<th scope='col' id='par' align='center'>Четврток</th>"));
        pnlPanela.Controls.Add(new LiteralControl("<th scope='col' id='nep' align='center'>Петок</th></tr></thead>"));
        pnlPanela.Controls.Add(new LiteralControl("<tbody>"));
        // for ciklus vo koj se izminuvaat redicite
        for (int i = 0; i < 12; i++)
        {
            pnlPanela.Controls.Add(new LiteralControl("<tr>"));
            bool imaPredavanje1 = false, imaPredavanje2 = false, imaPredavanje3 = false, imaPredavanje4 = false, imaPredavanje5 = false;
            int dolzinaNaBlokCas1 = 1, dolzinaNaBlokCas2 = 1, dolzinaNaBlokCas3 = 1, dolzinaNaBlokCas4 = 1, dolzinaNaBlokCas5 = 1;

            // for ciklus vo koj se izminuvaat site clenovi vo listata "od"
            for (int k = 0; k < od.Count; k++)
            {
                int predavanjeDen = Convert.ToInt32(den[k]); // se zema denot koga se odrzuva casot
                int predavanjeKraj= Convert.ToInt32(doo[k]); // se zema koga zavrsuva predavanjeto
                int predavanjePocetok = Convert.ToInt32(od[k]);// koga pocnuva predavanjeto
                if ((predavanjePocetok - 8) == i)// Dokolku casot pocnuva vo soodvetnoto vreme za dadenata redica se izvrshuva if-ot
                {
                    //Presmetka za kolku casa trae predavanjeto
                    int rez = predavanjeKraj - predavanjePocetok + 1; 
                    
                    //Za soodveten den gi izvrshuva naredbite, se setira imeto n profesorot ili laboratorsiki vezbi vo zavisnost od "c". Se setira imeto na predmetot. 
                    switch (predavanjeDen) 
                    {
                        case 1:
                            imaPredavanje1 = true;
                            dolzinaNaBlokCas1 = rez;
                            tipPredavanje1 = aupr[k];
                            predmet1 = predmet[k];
                            break;
                        case 2:
                            imaPredavanje2 = true;
                            dolzinaNaBlokCas2 = rez;
                            tipPredavanje2 = aupr[k];
                            predmet2 = predmet[k];
                            break;
                        case 3:
                            imaPredavanje3 = true;
                            dolzinaNaBlokCas3 = rez;
                            tipPredavanje3 = aupr[k];
                            predmet3 = predmet[k];
                            break;
                        case 4:
                            imaPredavanje4 = true;
                            dolzinaNaBlokCas4 = rez;
                            tipPredavanje4 = aupr[k];
                            predmet4 = predmet[k];
                            break;
                        case 5:
                            imaPredavanje5 = true;
                            dolzinaNaBlokCas5 = rez;
                            tipPredavanje5 = aupr[k];
                            predmet5 = predmet[k];
                            break;
                    }
                }
            }
            
            // Proveruva dali e blok casovi. Dokolku e, za soodvetniot brojac se setira vrednosta na brojot na casovi
            if (dolzinaNaBlokCas1 != 1) brojacDolzinaNaBlokCas1 = dolzinaNaBlokCas1;
            if (dolzinaNaBlokCas2 != 1) brojacDolzinaNaBlokCas2 = dolzinaNaBlokCas2;
            if (dolzinaNaBlokCas3 != 1) brojacDolzinaNaBlokCas3 = dolzinaNaBlokCas3;
            if (dolzinaNaBlokCas4 != 1) brojacDolzinaNaBlokCas4 = dolzinaNaBlokCas4;
            if (dolzinaNaBlokCas5 != 1) brojacDolzinaNaBlokCas5 = dolzinaNaBlokCas5;
            
            //Se dodava prvata kelija na redicite
            switch (i)
            {
                case 0: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>08:00-08:45</td>")); break;
                case 1: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>09:00-09:45</td>")); break;
                case 2: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>10:00-10:45</td>")); break;
                case 3: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>11:00-11:45</td>")); break;
                case 4: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>12:00-12:45</td>")); break;
                case 5: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>13:00-13:45</td>")); break;
                case 6: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>14:00-14:45</td>")); break;
                case 7: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>15:00-15:45</td>")); break;
                case 8: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>16:00-16:45</td>")); break;
                case 9: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>17:00-17:45</td>")); break;
                case 10: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>18:00-18:45</td>")); break;
                case 11: pnlPanela.Controls.Add(new LiteralControl("<td align='center'>19:00-19:45</td>")); break;
            }
            // proveruva dali ima cas so "imaPredavanje1-5". Ako ima kreira kelija i ja polni kelijata so imeto na "tipPredavanje1-5" i "predmet1-5". Ako nema proveruva brojacot sto gleda dali ima blok casovi dali e ednakov na nula. Ako e kreira prazna kelija
            if (imaPredavanje1) pnlPanela.Controls.Add(new LiteralControl("<td  align='center' rowspan=" + dolzinaNaBlokCas1 + "><h3>" + predmet1 + "<br/></h3><font color=#808080>" + tipPredavanje1 + "</font></td>"));
            else if (brojacDolzinaNaBlokCas1 == 0) pnlPanela.Controls.Add(new LiteralControl("<td></td>"));

            if (imaPredavanje2) pnlPanela.Controls.Add(new LiteralControl("<td align='center' rowspan=" + dolzinaNaBlokCas2 + "><h3>" + predmet2 + "<br/></h3><font color=#808080>" + tipPredavanje2 + "</font></td>"));
            else if (brojacDolzinaNaBlokCas2 == 0) pnlPanela.Controls.Add(new LiteralControl("<td></td>"));

            if (imaPredavanje3) pnlPanela.Controls.Add(new LiteralControl("<td align='center' rowspan=" + dolzinaNaBlokCas3 + "><h3>" + predmet3 + "<br/></h3><font color=#808080>" + tipPredavanje3 + "</font></td>"));
            else if (brojacDolzinaNaBlokCas3 == 0) pnlPanela.Controls.Add(new LiteralControl("<td></td>"));

            if (imaPredavanje4) pnlPanela.Controls.Add(new LiteralControl("<td align='center' rowspan=" + dolzinaNaBlokCas4 + "><h3>" + predmet4 + "<br/></h3><font color=#808080>" + tipPredavanje4 + "</font></td>"));
            else if (brojacDolzinaNaBlokCas4 == 0) pnlPanela.Controls.Add(new LiteralControl("<td></td>"));

            if (imaPredavanje5) pnlPanela.Controls.Add(new LiteralControl("<td align='center' rowspan=" + dolzinaNaBlokCas5 + "><h3>" + predmet5 + "<br/></h3><font color=#808080>" + tipPredavanje5 + "</font></td>"));
            else if (brojacDolzinaNaBlokCas5 == 0) pnlPanela.Controls.Add(new LiteralControl("<td></td>"));

            pnlPanela.Controls.Add(new LiteralControl("</tr>"));
            
            //Ako brojacot ne e ednakov na nula go namaluva
            if (brojacDolzinaNaBlokCas1 != 0) brojacDolzinaNaBlokCas1--;
            if (brojacDolzinaNaBlokCas2 != 0) brojacDolzinaNaBlokCas2--;
            if (brojacDolzinaNaBlokCas3 != 0) brojacDolzinaNaBlokCas3--;
            if (brojacDolzinaNaBlokCas4 != 0) brojacDolzinaNaBlokCas4--;
            if (brojacDolzinaNaBlokCas5 != 0) brojacDolzinaNaBlokCas5--;

        }//Zavrsuva forot za denovi. Nov den e :)
        pnlPanela.Controls.Add(new LiteralControl("</tbody>"));
        pnlPanela.Controls.Add(new LiteralControl("</table>"));

    }

    /// <summary>
    ///     <para>
    ///         Funckijata ispolni() vrakja objekt od tipot Niza, koj prethodno go polni so rasporedot za soodvetniot profesor.
    ///         Od bazata se zemaat site casovi (predavanja i konsultacii) za predmetite koi predava tekovniot profesor.
    ///         Se zema pocetnoto vreme, krajnoto vreme, denot na odrzuvanje, tipot na predavanje i imeto na predmetot.
    ///     </para>
    /// </summary>
    /// <returns></returns>
    protected Niza ispolni()
    {
        SqlConnection konekcija = new SqlConnection();
        konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
        SqlCommand komanda = new SqlCommand();
        komanda.Connection = konekcija;
        komanda.CommandText = "SELECT p.den, p.doo, p.od, pr.Predmet,p.AU_PR FROM Predava AS p, Predmet AS pr WHERE pr.p_ID=p.predmet_ID AND p.prof_ID='" +  Session["korisnicko"].ToString() + "'";
        List<string> den = new List<string>();
        List<string> od = new List<string>();
        List<string> doo = new List<string>();
        List<string> aupr = new List<string>();
        List<string> predmet = new List<string>();
        Niza lista = new Niza();
        try
        {
            konekcija.Open();
            SqlDataReader citac = komanda.ExecuteReader();

            while (citac.Read())
            {
                den.Add(citac["den"].ToString());
                od.Add(citac["od"].ToString());
                doo.Add(citac["doo"].ToString());
                if ((bool)citac["AU_PR"]) aupr.Add("Предавање");
                else aupr.Add("Аудиториски");
                predmet.Add(citac["Predmet"].ToString());
            }
            citac.Close();
            komanda.CommandText = "SELECT p.den, p.doo_k, p.ood_k FROM Profesori AS p WHERE p.a_KorIme='" + Session["korisnicko"].ToString() + "'";
            citac = komanda.ExecuteReader();

            while (citac.Read())
            {
                den.Add(citac["den"].ToString());
                od.Add(citac["ood_k"].ToString());
                doo.Add(citac["doo_k"].ToString());
                predmet.Add("Консултации");
                aupr.Add("");
            }
            citac.Close();
        }
        catch (Exception er) { }
        finally
        {
            lista.den = den;
            lista.doo = doo;
            lista.aupr = aupr;
            lista.predmet = predmet;
            lista.od = od;
            konekcija.Close();
        }
        return lista;
    }

    /// <summary>
    ///     <para>
    ///         Event handler za promena na terminot za konsultacii. Se postavuvaat na visible site kontroli potrebni za promena 
    ///         (den, poceten cas, kraen cas i dve kopcinja za otkazuvanje i odobruvanje na promenata)
    ///     </para>
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnKons_Click(object sender, EventArgs e)
    {
        btnKons.Visible = false;
        lblDen.Visible = true;
        lblDo.Visible = true;
        lblOd.Visible = true;
        txtDen.Visible = true;
        txtDo.Visible = true;
        txtOd.Visible = true;
        btnOtkazi.Visible = true;
        btnPromeni.Visible = true;
    }

    /// <summary>
    ///     <para>
    ///         Event handler za azuriranje na promenetiot termin za konsultacii.
    ///     </para>
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnPromeni_Click(object sender, EventArgs e)
    {
        int den=0;
        bool flag = true;
        int doo=Convert.ToInt32(txtDo.Text)-1;
        switch (txtDen.Text)
        {
            case "Ponedelnik": case "ponedelnik": case "понеделник": case "Понеделник": den=1; break;
            case "Vtornik": case "vtornik": case "Вторник": case "вторник": den=2; break;
            case "Sreda": case "sreda": case "Среда": case "среда": den=3; break;
            case "Cetvrtok": case "cetvrtok": case "Четврток": case "четврток": den=4; break;
            case "Petok": case "petok": case "петок": case "Петок": den=5; break;
        }
        lblProba.Text = den.ToString();
        SqlConnection konekcija = new SqlConnection();
        konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["mojaKonekcija"].ConnectionString;
        SqlCommand komanda = new SqlCommand();
        komanda.Connection = konekcija;
        komanda.CommandText = "SELECT * FROM Predava as p WHERE p.den='" + den.ToString() + "' AND p.prof_ID='" + Session["korisnicko"].ToString() + "' AND ((p.od>='" + txtOd.Text + "'AND p.od<='" + doo.ToString() + "') OR (p.doo>='" + txtOd.Text + "' AND p.doo<='" + doo.ToString() + "') OR (p.od<='" + txtOd.Text + "'AND p.doo>='" + doo.ToString() + "'))";
        
        try
        {
            konekcija.Open();
            SqlDataReader citac = komanda.ExecuteReader();
            //Dokolku citacot dobie barem eden red od pratenoto query znaci deka terminot e zafaten.
            if (citac.Read())
            {
                lblProba.Text = "Терминот не е слободен";
                flag = false;
            }
            citac.Close();
            if (flag)
            {
                komanda.CommandText = "Update Profesori SET den='" + den.ToString() + "', ood_k='" + txtOd.Text + "', doo_k='" + doo.ToString() + "' WHERE a_KorIme='" + Session["korisnicko"].ToString() + "'";
                komanda.ExecuteNonQuery();
            }
        }
        catch (Exception er) { lblProba.Text = "greska"; }
        finally
        {   
            //Se postavuvaat kontrolite za azuriranje da bidat nevidlivi.
            btnKons.Visible = true;
            lblDen.Visible = false;
            lblDo.Visible = false;
            lblOd.Visible = false;
            txtDen.Visible = false;
            txtDo.Visible = false;
            txtOd.Visible = false;
            btnOtkazi.Visible = false;
            btnPromeni.Visible = false;
            konekcija.Close();
            if(flag)Response.Redirect(Request.RawUrl); 
        }
    
        
    }

    /// <summary>
    ///     Event handler koj se povikuva dokolku se pritisne kopceto za otkazuvanje na azuriranje na terminot za konsultacii.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOtkazi_Click(object sender, EventArgs e)
    {
        txtDen.Text = string.Empty;
        txtDo.Text = string.Empty;
        txtOd.Text = string.Empty;
        btnKons.Visible = true;
        lblDen.Visible = false;
        lblDo.Visible = false;
        lblOd.Visible = false;
        txtDen.Visible = false;
        txtDo.Visible = false;
        txtOd.Visible = false;
        btnOtkazi.Visible = false;
        btnPromeni.Visible = false;
    }
}